/**
 * formCoordinate 转换坐标
 * @param roles
 * @param route
 */
function formCoordinate(imageInfo, data) {
  const template = data;
  if (!template) { return false; }
  // const coordinates = [
  //   {x:(template.aCoords.tl.x - imageInfo.left)/imageInfo.scale, y:(template.aCoords.tl.y - imageInfo.top)/imageInfo.scale}, // top left
  //   {x:(template.aCoords.tr.x - imageInfo.left)/imageInfo.scale, y:(template.aCoords.tr.y - imageInfo.top)/imageInfo.scale}, // top right
  //   {x:(template.aCoords.br.x - imageInfo.left)/imageInfo.scale, y:(template.aCoords.br.y - imageInfo.top)/imageInfo.scale}, // bottom right
  //   {x:(template.aCoords.bl.x - imageInfo.left)/imageInfo.scale, y:(template.aCoords.bl.y - imageInfo.top)/imageInfo.scale} // bottom left
  // ];
  const coordinates = [
    {x: Math.floor((Math.floor(template.aCoords.tl.x) - imageInfo.left)/imageInfo.scale), y: Math.floor((Math.floor(template.aCoords.tl.y) - imageInfo.top)/imageInfo.scale)}, // top left
    {x: Math.floor((Math.floor(template.aCoords.tr.x) - imageInfo.left)/imageInfo.scale), y: Math.floor((Math.floor(template.aCoords.tr.y) - imageInfo.top)/imageInfo.scale)}, // top right
    {x: Math.floor((Math.floor(template.aCoords.br.x) - imageInfo.left)/imageInfo.scale), y: Math.floor((Math.floor(template.aCoords.br.y) - imageInfo.top)/imageInfo.scale)}, // bottom right
    {x: Math.floor((Math.floor(template.aCoords.bl.x) - imageInfo.left)/imageInfo.scale), y: Math.floor((Math.floor(template.aCoords.bl.y) - imageInfo.top)/imageInfo.scale)} // bottom left
  ];
  return coordinates
}
/**
 * formCoordinateColumn 转换坐标
 * @param roles
 * @param route
 */
function formCoordinateColumn(imageInfo,template) {
  if (!template._objects) { return false; }
  const rectTemp = template._objects.filter(item => { return item.type === 'rect' })[0] ;
  const rectLeft =  Math.floor(template.left) -  Math.floor(imageInfo.left);
  const coordinates = []
  template._objects.map(item => {
    if (item.type === 'line') {
      coordinates.push(Math.floor((item.left + rectTemp.width/2 + rectLeft)/imageInfo.scale))
    }
  });
  // coordinates.pop();
  return coordinates
}
/**
 * crearAnchorTemplate 转换坐标
 * @param index
 * @param coordinates
 */
function crearAnchorTemplate (index, coordinates) {
  if (!coordinates || !index) { return false; }
  const template = {
    index,
    labelCategory: 'template',
    templateLabelInfo: {
      coordinates: coordinates,
      labelType: 'anchor',
      anchorLabelInfo: {
        anchorName: '',
        anchorSearchMethod: 'subStringMatch',
        anchorSearchExpression: ''
      }
    }
  }
  return template
}

/**
 * crearValueTemplate 转换坐标
 * @param index
 * @param coordinates
 */
function crearValueTemplate (index, coordinates) {
  if (!coordinates || !index) { return false; }
  const template = {
    index,
    labelCategory: 'template',
    templateLabelInfo: {
      coordinates: coordinates,
      labelType: 'value',
      valueLabelInfo: {
        valueFormatName: '',
        valueDefaultValue: '',
        valueMainAnchor: '',
        valueRegionChangeableFlag: false,
        valueChangeableRegionInfo: '',
        valueFormatMethod: 'originalText',
        valueGroupMethod: false,
        convertIntegerFormat: 0,
        convertFloatFormat: 0,
        detectCheckbox: [],
        regularExpression: {
          expression: '',
          groupId: ''
        }
      }
    }
  };
  return template
}

export default {
  namespaced: true,
  state: {
    snapshot: '',
    redoArr: [],
    undoArr: [],
    rectArr:[],
    tableArr:[],
    templateList: [],
    coordinatesList: [],
    imageInfo: {
      left: 0,
      top: 0,
      width: 0,
      height: 0
    },
  },
  mutations: {
    SAVE_SNAPSHOT (state, payload) {
      state.snapshot = payload;
    },
    SET_SNAPSHOT (state, type) {
      state.snapshot = (type === 'undo' ? state.undoArr : state.redoArr).pop();
    },
    CREAT_TEMPLATE_LIST (state, data) {
      const index = data.type.replace(/[^\d]/g, '') - 0;
      const coordinates = formCoordinate(state.imageInfo, data, index)
      // const anchorTemplate = crearAnchorTemplate(index, coordinates)
      state.coordinatesList.push(coordinates);
    },
    CHANGE_TEMPLATE_TYPE (state, index) {
      const templateList = state.templateList.filter(item =>{ return item.index !== index }); 
      let template = null; 
      if (type === 'anchor') {
        const coordinates = formCoordinate(state.imageInfo, data)
        template = crearAnchorTemplate(index, coordinates)
      } else if (type === 'value') {
        const coordinates = formCoordinate(state.imageInfo, data)
        template = crearValueTemplate(index, coordinates)
      }
      state.templateList = [...templateList, template];
    },
    SET_TABLE_LIST(state, data) {
      state.tableArr = data;
    },
    SET_UNDO_LIST (state) {
      state.undoArr.push(state.snapshot);
    },
    SET_REDO_LIST (state) {
      state.redoArr.push(state.snapshot);
    },
    SET_IMAGE_INFO (state, payload) {
      state.imageInfo = {...payload};
    },
    CLEAR_UNDO_LIST (state) {
      state.undoArr = [];
    },
    CLEAR_REDO_LIST (state) {
      state.redoArr = [];
    }
  },
  actions: {
    setCoordinatesList ({ commit, state }, payload) {
      const template = payload.filter(item => {return item.objectType !== 'copy'})
      state.coordinatesList = template.map(item => {
        const coordinates = formCoordinate(state.imageInfo, item);
        const columnLineCoordinateList = formCoordinateColumn(state.imageInfo, item);
        return {
          type:item.objectType,
          objectId:item.objectId,
          coordinates,
          columnLineCoordinateList
        }
      })
      console.log('coordinatesList', JSON.parse(JSON.stringify(state.coordinatesList)))
    }
  }
};